package com.aiks.dsyncpluginspringbootautoconfigure.plugin.module.user;


import com.aiks.dsyncpluginspringbootautoconfigure.enums.PluginEnum;
import com.aiks.dsyncpluginspringbootautoconfigure.plugin.AbstractConditionPlugin;
import com.aiks.dsyncpluginspringbootautoconfigure.plugin.ReflectUtils;
import com.aiks.dsyncpluginspringbootautoconfigure.plugin.module.UserModule;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.ModuleConfigEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgPlugParamsEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.SaasUserDTO;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * UserBlackConditionPlugin.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @date 2022-06-28 14:24
 * @since 1Abst
 **/
public class UserBlackConditionPlugin extends AbstractConditionPlugin<SaasUserDTO> implements UserModule {
    
    /**
     * getPluginEnum.
     *
     * @return PluginEnum
     */
    @Override
    public PluginEnum getPluginEnum() {
        return PluginEnum.UserBlackConditionPlugin;
    }
    
    /**
     * 用户属性黑名单过滤逻辑.
     *
     * @param moduleConfig           moduleConfig
     * @param saasUserDTO           saasUserDTO
     * @param orgPlugParamsEntities orgPlugParamsEntities
     * @return Boolean
     */
    @Override
    protected Boolean doFilter(final ModuleConfigEntity moduleConfig, final SaasUserDTO saasUserDTO,
                               final List<OrgPlugParamsEntity> orgPlugParamsEntities) {
        for (OrgPlugParamsEntity orgPlugParamsEntity : orgPlugParamsEntities) {
            Object fieldValue = ReflectUtils.INSTANCE.getFieldValue(saasUserDTO, orgPlugParamsEntity.getSaasFieldName());
            ArrayList<String> handleValues = Lists.newArrayList(orgPlugParamsEntity.getHandleValues().trim().split(","));
            if (handleValues.contains(String.valueOf(fieldValue))) {
                return true;
            }
        }
        return false;
    }
}
